package com.guigu.www.luolujin.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.www.luolujin.Service.LljKeepZongService;
import com.guigu.www.mapper.KeepInnerMapper;
import com.guigu.www.mapper.KeepZongMapper;
import com.guigu.www.mapper.TemplateKeepMapper;
import com.guigu.www.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 作者 罗露金
 */
@Service
public class LljKeepZongServiceImpl implements LljKeepZongService {
    @Autowired
    KeepZongMapper mapper;
    @Autowired
    KeepInnerMapper innerMapper;
    @Autowired
    TemplateKeepMapper keepMapper;

    @Override
    public PageVo<KeepZong> pageKeepZong(Integer pageNum, Integer pageSize, String keepZongTitle) {
        QueryWrapper<KeepZong> wrapper = new QueryWrapper<>();
        wrapper.like("keep_zong_title",keepZongTitle);
        Page page = new Page(pageNum, pageSize);
        IPage iPage = mapper.selectPage(page, wrapper);
        PageVo<KeepZong> PageVo = new PageVo<>();
        PageVo.setRows(iPage.getRecords());
        PageVo.setTotal(iPage.getTotal());
        return PageVo;
    }

    @Override
    public void updateKeepZong(Integer keepZongId, Integer keepZongState) {
        KeepZong keepZong = new KeepZong();
        keepZong.setKeepZongId(keepZongId);
        keepZong.setKeepZongState(keepZongState);
        mapper.updateById(keepZong);
    }

    @Override
    public void insertFoodZong(KeepZong keepZong, Integer[] tempKeepId) {
        mapper.insert(keepZong);
        for (Integer keepId : tempKeepId) {
            KeepInner keepInner = new KeepInner(null, keepZong.getKeepZongId(), keepId);
            innerMapper.insert(keepInner);
        }
    }

    @Override
    public List<TemplateKeep> queryById(int keepZongId) {
        return keepMapper.queryById(keepZongId);
    }
}
